package com.luxiu;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.io.File;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * 加载gif导致内存过大，加载慢，顺序播放图片代替 gif
 */
public class LoadingStartPage extends Application {

    private double sceneX;
    private double sceneY;
    private Scene scene;
    private Stage stage;
    private Stage transparentStage;
    private ImageView imageView;
    private Image[] images;
    private Thread loadingThread; // loading 线程
    private final Class self = LoadingStartPage.class;
    private final String loadingDir = "/loading";

    /**
     * 退出程序
     */
    public void exit() {
        stage.close();
        if (loadingThread != null) {
            loadingThread.interrupt();
        }
        if (transparentStage != null) {
            transparentStage.close();
        }
    }

    /**
     * 初始化界面
     */
    private void initFrame() {
        stage.initStyle(StageStyle.TRANSPARENT);
        stage.setWidth(440);
        stage.setHeight(324);
        BorderPane borderPane = new BorderPane();
        scene = new Scene(borderPane, Color.TRANSPARENT);
        // 与loading图片背景一致
        borderPane.setStyle("-fx-background-color: #2C3D4F");
        imageView = new ImageView();
        borderPane.getChildren().add(imageView);
        stage.setScene(scene);
    }

    /**
     * 设置拖拽
     */
    private void setDraggable() {
        this.scene.setOnMousePressed(event -> {
            sceneX = event.getSceneX();
            sceneY = event.getSceneY();
        });

        this.scene.setOnMouseDragged(event -> {
            // 最大化时不可拖拽
            if (!stage.isMaximized()) {
                stage.setX(event.getScreenX() - sceneX);
                stage.setY(event.getScreenY() - sceneY);
            }
        });
    }

    @Override
    public void start(Stage primaryStage) {
        // 隐藏任务栏图标
        transparentStage = new Stage(StageStyle.UTILITY);
        transparentStage.setOpacity(0);
        transparentStage.show();
        stage = primaryStage;
        stage.initOwner(transparentStage);
        initFrame();
        setDraggable();
        stage.show();
        // 异步加载图片
        CompletableFuture.runAsync(this::startLoading);
    }

    /**
     * 循环播放图片
     */
    private void playLoadingImages() {
        while (true) {
            for (Image image : images) {
                // 退出线程
                if (loadingThread.isInterrupted()) {
                    return;
                }
                imageView.setImage(image);
                try {
                    // 时间控制播放速度
                    Thread.sleep(40);
                } catch (InterruptedException e) {
                    // 重新设置interrupt
                    loadingThread.interrupt();
                }
            }
        }
    }

    /**
     * 开始 loading
     */
    private void startLoading() {
        // 图片数量
        int count = 251;
        images = new Image[count];
        for (int i = 0; i < count; i++) {
            String index = "" + i;
            if (i < 10) {
                index = "00" + index;
            } else if (i < 100) {
                index = "0" + index;
            }
            Image image = new Image(
                    self.getResourceAsStream(String.format("%s/%s.jpg", loadingDir, index)),
                    450,
                    324,
                    true,
                    true
            );
            images[i] = image;
            // 加载到10开始播放
            if (i == 10) {
                loadingThread = new Thread(this::playLoadingImages);
                loadingThread.start();
            }
        }
    }
}
